﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using YArchitech.LIB;

namespace YJKArch.AreaStatistical.DataManager
{
	public class RoomTypeSettingManager
	{
		public static bool ReadRoomTypeInfo(ref List<RoomSettingInfo> hallTypeList, ref List<RoomSettingInfo> roomTypeList, ref List<RoomSettingInfo> cellTypeList, ref List<RoomSettingInfo> otherTypeList)
		{
			string settingFilePath = RoomTypeSettingManager.GetSettingFilePath();
			if (!File.Exists(settingFilePath))
			{
				RoomTypeSettingManager.ReadDefaultInfo(ref hallTypeList, ref roomTypeList, ref cellTypeList, ref otherTypeList);
				return true;
			}
			XmlDocument xmlDocument = new XmlDocument();
			try
			{
				xmlDocument.Load(settingFilePath);
			}
			catch (Exception)
			{
				return false;
			}
			XmlNode xmlNode = xmlDocument.SelectSingleNode("Root");
			if (xmlNode == null)
			{
				return false;
			}
			RoomTypeSettingManager.ReadTypeGroup(xmlNode, "HallTypes", ref hallTypeList);
			RoomTypeSettingManager.ReadTypeGroup(xmlNode, "RoomTypes", ref roomTypeList);
			RoomTypeSettingManager.ReadTypeGroup(xmlNode, "CellTypes", ref cellTypeList);
			RoomTypeSettingManager.ReadTypeGroup(xmlNode, "OtherTypes", ref otherTypeList);
			return true;
		}

		public static bool WriteRoomTypeInfo(List<RoomSettingInfo> hallTypeList, List<RoomSettingInfo> roomTypeList, List<RoomSettingInfo> cellTypeList, List<RoomSettingInfo> otherTypeList)
		{
			string settingFilePath = RoomTypeSettingManager.GetSettingFilePath();
			XmlDocument xmlDocument = new XmlDocument();
			XmlNode xmlNode = xmlDocument.CreateNode(XmlNodeType.XmlDeclaration, "", "");
			XmlNode xmlNode2 = xmlNode;
			xmlNode2.InnerText += " encoding=\"GB2312\"";
			xmlDocument.AppendChild(xmlNode);
			XmlElement xmlElement = xmlDocument.CreateElement("", "Root", "");
			xmlDocument.AppendChild(xmlElement);
			RoomTypeSettingManager.WriteTypeGroup(xmlDocument, xmlElement, "HallTypes", hallTypeList);
			RoomTypeSettingManager.WriteTypeGroup(xmlDocument, xmlElement, "RoomTypes", roomTypeList);
			RoomTypeSettingManager.WriteTypeGroup(xmlDocument, xmlElement, "CellTypes", cellTypeList);
			RoomTypeSettingManager.WriteTypeGroup(xmlDocument, xmlElement, "OtherTypes", otherTypeList);
			bool result;
			try
			{
				xmlDocument.Save(settingFilePath);
				result = true;
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				result = false;
			}
			return result;
		}

		private static bool ReadTypeGroup(XmlNode rootNode, string groupName, ref List<RoomSettingInfo> typeList)
		{
			XmlNode xmlNode = rootNode.SelectSingleNode(groupName);
			if (xmlNode == null)
			{
				return false;
			}
			foreach (object obj in xmlNode.ChildNodes)
			{
				XmlElement xmlElement = ((XmlNode)obj) as XmlElement;
				string attribute = xmlElement.GetAttribute("Name");
				try
				{
					double coefficient = Convert.ToDouble(xmlElement.GetAttribute("Coefficient"));
					bool isBalcony = Convert.ToBoolean(xmlElement.GetAttribute("IsBalcony"));
					typeList.Add(new RoomSettingInfo(attribute, coefficient, isBalcony));
				}
				catch (Exception)
				{
					double coefficient2 = 1.0;
					bool isBalcony2 = false;
					if (attribute.CompareTo("阳台") == 0)
					{
						coefficient2 = 0.5;
						isBalcony2 = true;
					}
					typeList.Add(new RoomSettingInfo(attribute, coefficient2, isBalcony2));
				}
			}
			return true;
		}

		private static void WriteTypeGroup(XmlDocument xmlDoc, XmlNode rootNode, string groupName, List<RoomSettingInfo> typeList)
		{
			XmlElement xmlElement = xmlDoc.CreateElement("", groupName, "");
			rootNode.AppendChild(xmlElement);
			foreach (RoomSettingInfo roomSettingInfo in typeList)
			{
				XmlElement xmlElement2 = xmlDoc.CreateElement("Type");
				xmlElement2.SetAttribute("Name", roomSettingInfo.m_Name);
				XmlElement xmlElement3 = xmlElement2;
				string name = "Coefficient";
				double coefficient = roomSettingInfo.m_Coefficient;
				xmlElement3.SetAttribute(name, coefficient.ToString());
				XmlElement xmlElement4 = xmlElement2;
				string name2 = "IsBalcony";
				bool isBalcony = roomSettingInfo.m_IsBalcony;
				xmlElement4.SetAttribute(name2, isBalcony.ToString());
				xmlElement.AppendChild(xmlElement2);
			}
		}

		private static string GetSettingFilePath()
		{
			return Product.WorkingLocation + "RoomTypeSettingInfo.xml";
		}

		private static void ReadDefaultInfo(ref List<RoomSettingInfo> hallTypeList, ref List<RoomSettingInfo> roomTypeList, ref List<RoomSettingInfo> cellTypeList, ref List<RoomSettingInfo> otherTypeList)
		{
			hallTypeList.Add(new RoomSettingInfo("客厅", 1.0, false));
			hallTypeList.Add(new RoomSettingInfo("大厅", 1.0, false));
			hallTypeList.Add(new RoomSettingInfo("多功能厅", 1.0, false));
			hallTypeList.Add(new RoomSettingInfo("厅", 1.0, false));
			hallTypeList.Add(new RoomSettingInfo("餐厅", 1.0, false));
			hallTypeList.Add(new RoomSettingInfo("起居室", 1.0, false));
			hallTypeList.Add(new RoomSettingInfo("会客室", 1.0, false));
			roomTypeList.Add(new RoomSettingInfo("卧室", 1.0, false));
			roomTypeList.Add(new RoomSettingInfo("卧", 1.0, false));
			roomTypeList.Add(new RoomSettingInfo("主卧室", 1.0, false));
			roomTypeList.Add(new RoomSettingInfo("主卧", 1.0, false));
			roomTypeList.Add(new RoomSettingInfo("次卧室", 1.0, false));
			roomTypeList.Add(new RoomSettingInfo("次卧", 1.0, false));
			roomTypeList.Add(new RoomSettingInfo("客卧室", 1.0, false));
			roomTypeList.Add(new RoomSettingInfo("客卧", 1.0, false));
			cellTypeList.Add(new RoomSettingInfo("卫生间", 1.0, false));
			cellTypeList.Add(new RoomSettingInfo("化妆间", 1.0, false));
			cellTypeList.Add(new RoomSettingInfo("盥洗室", 1.0, false));
			cellTypeList.Add(new RoomSettingInfo("厕所", 1.0, false));
			cellTypeList.Add(new RoomSettingInfo("厕", 1.0, false));
			cellTypeList.Add(new RoomSettingInfo("浴室", 1.0, false));
			cellTypeList.Add(new RoomSettingInfo("浴", 1.0, false));
			otherTypeList.Add(new RoomSettingInfo("厨房", 1.0, false));
			otherTypeList.Add(new RoomSettingInfo("阳台", 0.5, true));
			otherTypeList.Add(new RoomSettingInfo("储藏间", 1.0, false));
			otherTypeList.Add(new RoomSettingInfo("走廊", 1.0, false));
		}
	}
}
